package com.shengyin.ec.controller.shopcart;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.WebUtils;

import com.shengyin.ec.controller.order.OrderDetailForm;
import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.Delivery;
import com.shengyin.ec.model.entity.Goods;
import com.shengyin.ec.model.entity.Payment;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.model.entity.Receiver;
import com.shengyin.ec.service.account.ReceiverService;
import com.shengyin.ec.service.delivery.DelieveryService;
import com.shengyin.ec.service.goods.GoodsService;
import com.shengyin.ec.service.payment.PaymentService;

@Controller
public class ShopCartController {

	@Autowired
	GoodsService goodsService;

	@Autowired
	PaymentService paymentService;

	@Autowired
	DelieveryService deliveryService;

	@Autowired
	ReceiverService receiverService;

	/*
	 * 进入购物车
	 */
	@RequestMapping("/toShopCart.do")
	public String toShopCart(HttpServletRequest request, ModelMap modelMap) {

		String goodsid = request.getParameter("goodsid");
		String prodcutquantity = request.getParameter("prodcutQuantity");

		ShopCartForm shopCart = (ShopCartForm) WebUtils.getSessionAttribute(
				request, "shopCart");

		// 创建购物车
		if (shopCart == null) {
			shopCart = new ShopCartForm();
		}

		if (goodsid != null && prodcutquantity != null) {

			Long goodsId = Long.valueOf(goodsid);
			Integer prodcutQuantity = Integer.valueOf(prodcutquantity);

			// 获得商品
			Goods goods = goodsService.getGoodsById(goodsId);

			// 创建购物车项目
			Item item = new Item(goods, prodcutQuantity);

			// 添加记录到购物车
			shopCart.addItem(item);
			shopCart.caculateTotalPrice(item);
			shopCart.caculateTotalGoodsNum(item);
			shopCart.caculateTotalMarketPrice(item);

			WebUtils.setSessionAttribute(request, "shopCart", shopCart);
			modelMap.addAttribute("itemList", shopCart.getItemList());
			modelMap.addAttribute("productTotal",
					shopCart.getProductTotalQuantity());
			return "/view/shopcart/shopcart";
		}
		shopCart.caculateTotalPrice(null);
		shopCart.caculateTotalMarketPrice(null);
		shopCart.caculateTotalGoodsNum(null);
		modelMap.addAttribute("itemList", shopCart.getItemList());
		modelMap.addAttribute("productTotal",
				shopCart.getProductTotalQuantity());
		return "/view/shopcart/shopcart";

	}

	@RequestMapping("/view/shopCartDelete.do")
	public String shopCartDelete(@RequestParam("id") Long id,
			HttpServletRequest request, ModelMap modelMap) {
		ShopCartForm shopCart = (ShopCartForm) WebUtils.getSessionAttribute(
				request, "shopCart");

		Goods goods = goodsService.getGoodsById(id);

		if (shopCart != null && goods != null) {
			shopCart.remove(goods);

			modelMap.addAttribute("productTotal",
					shopCart.getProductTotalQuantity());
			modelMap.addAttribute("itemList", shopCart.getItemList());
			return "/view/shopcart/shopcart";
		}
		return "redirect:/tonIdex.do";
	}

	@RequestMapping("/view/shopCartUpdate.do")
	public String updateShopCart(HttpServletRequest request, ModelMap modelMap,
			@RequestParam("id") Long id, @RequestParam("quantity") int quantity) {
		ShopCartForm shopCart = (ShopCartForm) WebUtils.getSessionAttribute(
				request, "shopCart");


		
		shopCart.updateQuantity(quantity, id);
		
		shopCart.caculateTotalPrice(null);
		shopCart.caculateTotalMarketPrice(null);
		shopCart.caculateTotalGoodsNum(null);

		modelMap.addAttribute("productTotal",
				shopCart.getProductTotalQuantity());
		modelMap.addAttribute("itemList", shopCart.getItemList());
		return "/view/shopcart/shopcart";
	}

	@RequestMapping("/view/toAccountingCenter.do")
	public String AccountingCenter(HttpServletRequest request, ModelMap model,
			OrderDetailForm orderDetailForm) {
		Personal user = (Personal) WebUtils
				.getSessionAttribute(request, "user");

		List<Payment> paymentList = paymentService.getPaymentList();

		List<Delivery> deliveryList = deliveryService.getDelieveryList();
		ShopCartForm shopCart = (ShopCartForm) WebUtils.getSessionAttribute(
				request, "shopCart");
		//String[] ids = (String[]) request.getParameterValues("goodsIds");

		if (shopCart == null) {
		
			return "redirect:/toShopCart.do";
		}

		if (user == null)
			return "/view/login";

		List<Receiver> receiverList = receiverService.getReceiverListById(user
				.getId());

		if (receiverList == null || receiverList.size() == 0)
			return "redirect:/view/order/receiverInfoAdd.jsp";

		//List<Goods> goodsList = goodsService.getGoodsListBySelected(ids);

		List<Item> itemList = shopCart.getItemList();

		shopCart.caculateBoughtItemPrice(itemList);
		shopCart.caculateBoughtItemMarketPrice(itemList);
		model.addAttribute("itemList", itemList);
		model.addAttribute("deliveryList", deliveryList);
		model.addAttribute("paymentList", paymentList);
		model.addAttribute("receiverList", receiverList);
		return "/view/order/orderInfo";
	}

	/*
	 * spring mvc异常处理方法，在controller发生异常，可以以抛出ControllerException，以供捕获
	 */
	@ExceptionHandler({ ControllerException.class })
	public String exception(ControllerException e, HttpServletRequest request) {
		request.setAttribute("error", e.getMessage());
		return "/admin/error/error";
	}
}
